<?php

// +-----------------------------------------------
// | Name: 星空框架 权限节点菜单服务
// +-----------------------------------------------
// | Version: V5.0
// +-----------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +-----------------------------------------------
// | Time: 2021-04-20 18:36
// +-----------------------------------------------
declare(strict_types=1);

namespace app\common\services;

use app\common\model\AuthRule as AuthRuleModel;
use app\common\model\Menu as MenuModel;
use app\common\model\Cate as CateModel;
use think\facade\Session;

/**
 * @xk 权限节点菜单服务
 */
class AuthService
{
    /**
     * @xk 获取左侧菜单
     *
     * @method getSystemMenu
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-20T18:38:03+080
     */
    public static function getSystemMenu()
    {
        $admin_num = Session::has('admin_num') ? Session::get('admin_num') : '';
        $suppler_admin_num = config('manage.suppler_admin_num');
        switch ($admin_num) {
            case $suppler_admin_num:
                //超级管理员
                $list = self::getSupplerMenu();
                break;
            default:
                //普通管理员
                $list = self::getMenu();
                break;
        }

        return $list ? $list : '';
    }
    /**
     * @xk 超级管理获取菜单
     *
     * @method getSupplerMenu
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-20T18:38:34+080
     */
    public static function getSupplerMenu()
    {
        $menuModel = new MenuModel();
        $cateModel = new CateModel();
        $system_menu_list = $menuModel
                            ->where(['status' => 1])
                            ->field('`menu_id`,`icon`,`parent_id`,`menu_name`,`sign`,`linkurl`')
                            ->order('order_id asc')
                            ->select()
                            ->toArray(); // 全部菜单
        $system_menu = self::getTree($system_menu_list);
        // 开始获取栏目信息
        // 获取后台菜单的树形图
        $cate_list = [];
        $cate_list['title'] = '内容';
        $cate_list['icon'] = 'fa fa-hourglass-2';
        $cate_list['href'] = '';
        $cate_list['target'] = '_self';
        $cate = $cateModel
                ->where(['status' => 1])
                ->field('`cate_id`,`parent_id`,`cate_name`,`linkurl_back`')
                ->order('order_id asc')
                ->select()
                ->toArray();
        if ($cate) {
            $cate_list['child'] = self::getCateTree($cate);
        }
        if ($cate_list) {
            $system_menu[] = $cate_list;
        }
        return $system_menu;
    }

    /**
     * @xk 非超级管理员获取权限
     *
     * @method getMenu
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-19T10:43:58+080
     */
    public static function getMenu()
    {
        $authRuleModel = new AuthRuleModel();
        $cateModel = new CateModel();
        $auth_rule_id = Session::has('auth_rule_id') ? explode(',', Session::get('auth_rule_id')) : ''; //获取权限节点id
        $auth_rule_links = [];
        $auth_rule_links = $authRuleModel
                      ->where([['auth_rule_id', 'in', $auth_rule_id]])
                      ->column('linkurl');
        if ($auth_rule_links) {
            Session::set('auth_rule_links', $auth_rule_links); //将已经组合完毕的权限节点写入到权限节点族中
        }
        $menuModel = new MenuModel();
        $system_menu_list = $menuModel->where(['status' => 1])->field('`menu_id`,`icon`,`parent_id`,`menu_name`,`sign`,`linkurl`')->order('order_id asc')->select()
      ->toArray(); // 全部菜单
        if ($system_menu_list) {
            foreach ($system_menu_list as $key => $value) {
                if (!in_array(strtolower($value['linkurl']), $auth_rule_links)) {
                    unset($system_menu_list[$key]); //如果不在规则允许的范围内 则直接进行卸载
                }
            }
        }
        $system_menu = self::getTree($system_menu_list);
        // 开始获取栏目信息
        // 获取后台菜单的树形图
        $cate_list = [];
        $cate_list['title'] = '内容';
        $cate_list['icon'] = 'fa fa-hourglass-2';
        $cate_list['href'] = '';
        $cate_list['target'] = '_self';
        $cate = $cateModel
                ->where(['status' => 1])
                ->field('`cate_id`,`parent_id`,`cate_name`,`linkurl_back`')
                ->order('order_id asc')
                ->select()
                ->toArray();
        if ($cate) {
            $cate_list['child'] = self::getCateTree($cate);
        }
        if ($cate_list) {
            $system_menu[] = $cate_list;
        }
        return $system_menu;
    }

    /**
     * @xk 获取菜单树形
     *
     * @method getTree
     *
     * @param [type] $datalist [description]
     * @param int    $pid      [description]
     * @param array  $hasNode  [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-20T18:40:39+080
     */
    public static function getTree($datalist, $pid = 0, $hasNode = [])
    {
        $tree = [];
        if ($datalist) {
            foreach ($datalist as $key => $value) {
                if ('cate' == $value['sign']) {
                    continue;
                }
                if ($value['parent_id'] == $pid) {
                    $auth_rule['title'] = $value['menu_name'];
                    $auth_rule['icon'] = $value['icon'];
                    if ($value['linkurl']) {
                        $value['linkurl'] = '/'.config('manage.manage_name').'/'.config('manage.version').'/'.$value['linkurl'];
                    }
                    $auth_rule['href'] = $value['linkurl'];
                    $auth_rule['target'] = '_self';
                    $auth_rule['child'] = self::getTree($datalist, $value['menu_id'], $hasNode);
                    if (!$auth_rule['child']) {
                        unset($auth_rule['child']);
                    }
                    $tree[] = $auth_rule;
                }
            }
        }

        return $tree;
    }
    /**
     * @xk 获取栏目树形图
     *
     * @method getCateTree
     * @param  [type]                  $datalist                 [description]
     * @param  integer                 $pid                      [description]
     * @param  array                   $hasNode                  [description]
     * @return [type]                                            [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-28T18:14:06+080
     */
    public static function getCateTree($datalist, $pid = 0, $hasNode = [])
    {
        $tree = [];
        if ($datalist) {
            foreach ($datalist as $key => $value) {
                if ($value['parent_id'] == $pid) {
                    $auth_rule['title'] = $value['cate_name'];
                    $auth_rule['icon'] = 'fa fa-hourglass-2';
                    $auth_rule['href'] = $value['linkurl_back'];
                    if ($value['linkurl_back']) {
                        $auth_rule['href'] = '/jpyepmc/v1/'.$value['linkurl_back'].'/cate_id/'.$value['cate_id'];
                    }
                    $auth_rule['target'] = '_self';
                    $auth_rule['child'] = self::getCateTree($datalist, $value['cate_id'], $hasNode);
                    if (!$auth_rule['child']) {
                        unset($auth_rule['child']);
                    } else {
                        $auth_rule['href'] = '';
                    }
                    $tree[] = $auth_rule;
                }
            }
        }

        return $tree;
    }
}
